package com.gxg.dao;

import com.gxg.entities.ExperimentalDocument;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class ExperimentalDocumentDao
{
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public void createExperimentalDocumental(String id, String title, String name, String courseId, Timestamp createTime, Timestamp modificationTime)
  {
    String sql = "insert into ExperimentalDocument values(?, ?, ?, ?, ?, ?)";
    this.jdbcTemplate.update(sql, new Object[] { id, title, name, courseId, createTime, modificationTime });
  }
  
  public List<ExperimentalDocument> getExperimentalDocumentByCourseId(String courseId)
  {
    if (getExperimentalDocumentCountByCourseId(courseId) == 0) {
      return null;
    }
    String sql = "select * from ExperimentalDocument where courseId=? order by id";
    List<ExperimentalDocument> experimentalDocumentList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public ExperimentalDocument mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalDocument experimentalDocument = new ExperimentalDocument();
        experimentalDocument.setId(resultSet.getString("id"));
        experimentalDocument.setTitle(resultSet.getString("title"));
        experimentalDocument.setName(resultSet.getString("name"));
        experimentalDocument.setCourseId(resultSet.getString("courseId"));
        experimentalDocument.setCreateTime(resultSet.getTimestamp("createTime"));
        experimentalDocument.setModificationTime(resultSet.getTimestamp("modificationTime"));
        return experimentalDocument;
      }
    }, new Object[] { courseId });
    
    return experimentalDocumentList;
  }
  
  public int getExperimentalDocumentCountByCourseId(String courseId)
  {
    String sql = "select count(*) from ExperimentalDocument where courseId=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { courseId })).intValue();
    return rowCount;
  }
  
  public List<ExperimentalDocument> getExperimentalDocumentTop5ByCourseId(String courseId)
  {
    if (getExperimentalDocumentCountByCourseId(courseId) == 0) {
      return null;
    }
    String sql = "select * from ExperimentalDocument where courseId=? order by id limit 0, 5";
    List<ExperimentalDocument> experimentalDocumentList = this.jdbcTemplate.query(sql, new RowMapper()
    {
      public ExperimentalDocument mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalDocument experimentalDocument = new ExperimentalDocument();
        experimentalDocument.setId(resultSet.getString("id"));
        experimentalDocument.setTitle(resultSet.getString("title"));
        experimentalDocument.setName(resultSet.getString("name"));
        experimentalDocument.setCourseId(resultSet.getString("courseId"));
        experimentalDocument.setCreateTime(resultSet.getTimestamp("createTime"));
        experimentalDocument.setModificationTime(resultSet.getTimestamp("modificationTime"));
        return experimentalDocument;
      }
    }, new Object[] { courseId });
    
    return experimentalDocumentList;
  }
  
  public int getExperimentalDocumentCountById(String id)
  {
    String sql = "select count(*) from experimental_document where id=?";
    int rowCount = ((Integer)this.jdbcTemplate.queryForObject(sql, Integer.class, new Object[] { id })).intValue();
    return rowCount;
  }
  
  public ExperimentalDocument getExperimentalDocumentById(String id)
  {
    if (getExperimentalDocumentCountById(id) == 0) {
      return null;
    }
    String sql = "select * from ExperimentalDocument where id=?";
    ExperimentalDocument experimentalDocument = (ExperimentalDocument)this.jdbcTemplate.queryForObject(sql, new RowMapper()
    {
      public ExperimentalDocument mapRow(ResultSet resultSet, int i)
        throws SQLException
      {
        ExperimentalDocument experimentalDocument = new ExperimentalDocument();
        experimentalDocument.setId(resultSet.getString("id"));
        experimentalDocument.setTitle(resultSet.getString("title"));
        experimentalDocument.setName(resultSet.getString("name"));
        experimentalDocument.setCourseId(resultSet.getString("courseId"));
        experimentalDocument.setCreateTime(resultSet.getTimestamp("createTime"));
        experimentalDocument.setModificationTime(resultSet.getTimestamp("modificationTime"));
        return experimentalDocument;
      }
    }, new Object[] { id });
    
    return experimentalDocument;
  }
  
  public void deleteExperimentalDocumentById(String id)
  {
    String sql = "delete from ExperimentalDocument where id=?";
    this.jdbcTemplate.update(sql, new Object[] { id });
  }
  
  public void updateTitleAndModificationTimeById(String id, String title, Timestamp time)
  {
    String sql = "update ExperimentalDocument set title=?, modificationTime=? where id=?";
    this.jdbcTemplate.update(sql, new Object[] { title, time, id });
  }
  
  public void updateModificationTimeById(String id, Timestamp time)
  {
    String sql = "update ExperimentalDocument set modificationTime=? where id=?";
    this.jdbcTemplate.update(sql, new Object[] { time, id });
  }
}
